﻿using Academy.Core.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using System.Data.Entity;

namespace Academy.Data
{
    public class Repository<T> : IRepository<T> where T : class, new()
    {
        protected readonly DbContext context;

        public Repository(IDbContextFactory f)
        {
            context = f.GetContext();
        }

        public void Delete(T item)
        {
            throw new NotImplementedException();
        }

        public T Get(int id)
        {
            var entity = context.Set<T>().Find(id);
            if (entity == null) throw new Exception("this entity doesn't exist anymore");
            return entity;
        }

        public IQueryable<T> GetAll()
        {
            throw new NotImplementedException();
        }

        public T Insert(T item)
        {
            var t = context.Set<T>().Create();
            context.Set<T>().Add(item);
            return t;
        }

        public void Restore(T item)
        {
            throw new NotImplementedException();
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public IQueryable<T> Where(Expression<Func<T, bool>> predicate, bool showDeleted = false)
        {
            throw new NotImplementedException();
        }
    }
}
